package com.wcs.exam.common.server.sms;

import com.wcs.exam.common.base.BaseBiz;
import com.wcs.exam.common.base.exception.BaseException;
import com.wcs.exam.common.enums.SmsPlatformEnum;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Map;

@RequiredArgsConstructor
@Component
public class SmsService extends BaseBiz {

    @NotNull
    private final Map<String, SmsFace> smsFaceMap;

    /**
     * 发送短信验证码
     *
     * @param mobile 手机
     * @param code   验证码
     * @param sms    短信配置
     * @return 发送结果
     */
    public Boolean sendVerCode(Integer smsPlatform, String mobile, String code, Sms sms) {
        SmsFace smsFace = getSmsFace(smsPlatform);
        return smsFace.sendVerCode(mobile, code, sms);
    }

    /**
     * 获取短信通道
     *
     * @param smsPlatform 短信平台
     * @return 短信通道
     */
    private SmsFace getSmsFace(Integer smsPlatform) {
        SmsPlatformEnum smsPlatformEnum = SmsPlatformEnum.byCode(smsPlatform);
        if (smsPlatformEnum == null) {
            throw new BaseException("短信平台错误");
        }
        SmsFace smsFace = smsFaceMap.get(smsPlatformEnum.getMode());
        if (smsFace == null) {
            throw new BaseException("短信平台错误");
        }
        return smsFace;
    }
}
